//
//  GBMonitorTableview.m
//  PublicSentimentMonitorSystem
//
//  Created by 郭斌 on 15/11/4.
//  Copyright © 2015年 魏强. All rights reserved.
//

#import "GBMonitorTableview.h"
#import "GBMonitorTableViewCell.h"

@interface GBMonitorTableview () <UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *dataArray;
    NSMutableArray *dataIDArray; //数据的id
    NSMutableArray *selectDataarray; //选中的数组
    NSMutableArray *selectDataIDarray; //选中的id数组
    NSMutableArray *statusArray; //数组的状态
}
@end

@implementation GBMonitorTableview
-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        selectDataarray = [NSMutableArray array];
        selectDataIDarray = [NSMutableArray array];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initData:) name:@"DataArrayNickname" object:nil];
        //添加关键字
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(AddNewData:) name:@"AddTheKeyword" object:nil];
        //删除关键字
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteWithCell:) name:@"DeleteTheKeyword" object:nil];
        
        self.rowHeight = 40;
        self.tableFooterView = [[UIView alloc] init];
        self.delegate = self;
        self.dataSource = self;
        [self registerClass:[GBMonitorTableViewCell class] forCellReuseIdentifier:@"Cell"];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteWithCell:) name:@"DeleteTheKeyword" object:nil];
        
        [self sendTheDataArr];
    }
    return self;
}

#pragma mark - 初始化数据
-(void)initData:(NSNotification *)note
{
    NSDictionary *user = [note userInfo];
    NSMutableArray *arrr = [user objectForKey:@"dataArrayNickname"];
    NSMutableArray *arrrID = [user objectForKey:@"dataArrayNicknameID"];
    
    dataArray = [NSMutableArray arrayWithArray:arrr];
    dataIDArray = [NSMutableArray arrayWithArray:arrrID];
    
    //初始化原始状态数组
    statusArray = [NSMutableArray array];
    for(int i = 0; i < dataArray.count; i++)
    {
        [statusArray addObject:@"未选"];
    }
}

#pragma mark - 添加新数据
-(void)AddNewData:(NSNotification *)note
{
    NSDictionary *user = [note userInfo];
    int position = [[user objectForKey:@"deleteBtnTag"] intValue] - 1000;
    //NSLog(@"%d", position);
    //改变状态
    [statusArray replaceObjectAtIndex:position withObject:@"已选"];
    [selectDataarray insertObject:[dataArray objectAtIndex:position] atIndex:0];
    [selectDataIDarray insertObject:[dataIDArray objectAtIndex:position] atIndex:0];
    
    //NSLog(@"11%@", selectDataarray);
    //发送新的数据
    [self sendTheDataArr];
}

#pragma mark - 删除按钮，刷新tableview
-(void)deleteWithCell:(NSNotification *)note
{
    NSDictionary *user = [note userInfo];
    int position = [[user objectForKey:@"deleteBtnTag"] intValue] - 1000;
    //改变状态
    [statusArray replaceObjectAtIndex:position withObject:@"未选"];
    
    NSString *deleteName = [dataArray objectAtIndex:position];
    NSString *deleteId = [dataIDArray objectAtIndex:position];
    //找到删除的位置
    [selectDataarray removeObject:deleteName];
    [selectDataIDarray removeObject:deleteId];
    
    //NSLog(@"22%@", selectDataarray);
    //发送新的数据
    [self sendTheDataArr];
}

#pragma mark - 发送数据
-(void)sendTheDataArr
{
    //发送新的数据
    NSDictionary *userInfo11b = [[NSDictionary alloc] initWithObjectsAndKeys:selectDataarray, @"deleteArray", selectDataIDarray, @"deleteIDArray",nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NewArray" object:nil userInfo:userInfo11b];
}

#pragma mark - tableview返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    GBMonitorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    cell.titleLab.text = [NSString stringWithFormat:@"%@", dataArray[indexPath.row]];
    cell.CancleBtn.tag = indexPath.row + 1000;
    
    if([statusArray[indexPath.row] isEqualToString:@"已选"])
    {
        cell.CancleBtn.selected = YES;
    }
    else if([statusArray[indexPath.row] isEqualToString:@"未选"])
    {
        cell.CancleBtn.selected = NO;
    }
    
    //定制cell右按钮
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
